<?php

require_once("page.class.php");
require_once("functions.php");

class evidencija_dolazaka extends page{
	
	var $_content;
	var $rows=array();
	
	var $weekdays = array("ponedeljak","utorak","sreda","četvrtak","petak","subota","nedelja");
	var $sweekdays= array("pon","uto","sre","čet","pet","sub","ned");
	
	var $escapedWorkers = array("Kancelarija","Upis sa interneta","upis iz novina","vizit karta","radnik1","radnik2","radnik3","radnik4","radnik5","radnik6");
	
	
	function evidencija_dolazaka(){
		$this->initPage();
		$this->db = new db_functions();
		
		if (!$this->isUserLoggedIn()) {
			$this->_container = "login.tpl";
			$this->displayPage();
			die();
		}
		
		$action = getFromInput("a");
		switch ($action){
			case "browse":
				echo $this->browse();
				break;
			case "save":
				$this->save();
				$this->_main(1);
				break;
			case "monthlyReport":
				$this->monthlyReport();
				break;
			case "delete":
				$this->deleteEvidencija();
				break;
			case "subote":
				$this->suboteReport();
				break;
			default:
				$this->_main();
				break;
		}
	}
	
	function _main($msg=0){
		$nongui = getFromInput("nongui");
		
		$this->_content = "evidencija_dolazaka/start.tpl";
		$rad = $this->get_Radnici(null,1);
			$this->assign("radnik_id",$rad["polje_id"]);
			$this->assign("radnik_ime",$rad["polje_ime"]);
			$this->assign("radnik_prezime",$rad["polje_prezime"]);
		$opr = $this->get_EvidencijaOpravdanja();
			$this->assign("opravdanje_id",$opr["polje_id"]);
			$this->assign("opravdanje_name",$opr["polje_ime"]);
			
				
		if ($nongui==1) {
			echo $this->parseTemplate("evidencija_dolazaka/start.tpl");
		}else{
			$this->spitGUIPage();
		}
	}
	function browse(){
		$fRadnik = getFromInput("filter_radnik");
		$fGrupa = getFromInput("filter_gruprad");
		$fDate = getFromInput("filter_date");
		$fOpravdanje = getFromInput("filter_opravdanje");
		
		$monthly=false;
		if (preg_match('/^(\d{1,2}\.)+\d{2,4}$/',$fDate)) {
			$this->assign("filter_date",$fDate);
			$tmp = explode(".",$fDate);
			if (count($tmp)>2) {
				$fDate_start = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				$fDate_end = makeUnixTime(0,0,0,$tmp[1],($tmp[0]+1),$tmp[2]);
			}else{
				$monthly=true;
				$fDate_start = makeUnixTime(0,0,0,$tmp[0],1,$tmp[1]);
				$fDate_end = makeUnixTime(0,0,0,($tmp[0]+1),1,$tmp[1]);
			}
		}else{
			$day = date("d"); $month = date("m"); $year = date("Y");
			$fDate_start = makeUnixTime(0,0,0,$month,$day,$year);
			$fDate_end = makeUnixTime(0,0,0,$month,($day+1),$year);
			$this->assign("filter_date",date("d.m.Y",$fDate_start));
		}
		$opr = $this->get_EvidencijaOpravdanja($fOpravdanje);
			$this->assign("opravdanje_id",$opr["polje_id"]);
			$this->assign("opravdanje_name",$opr["polje_ime"]);
			$this->assign("opravdanje_selected",$opr["polje_selected"]);
		$groups = $this->get_RadniciGrupe($fGrupa);
			$this->assign("group_id",$groups["polje_id"]);
			$this->assign("group_name",$groups["polje_ime"]);
			$this->assign("group_selected",$groups["polje_selected"]);
		$rad = $this->get_Radnici($fRadnik,1);
			$this->assign("radnik_id",$rad["polje_id"]);
			$this->assign("radnik_ime",$rad["polje_ime"]);
			$this->assign("radnik_prezime",$rad["polje_prezime"]);
			$this->assign("radnik_selected",$rad["polje_selected"]);
		
		if ($monthly && $fRadnik<1) {
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Odaberite radnika.");
		}else{
			$aktivni_radnici = $this->get_Radnici(null,1);
			
			if ($fRadnik>0) {
				list($fRadnikIme,$fRadnikPrezime) = $this->db->getRow("SELECT radnik_ime,radnik_prezime FROM radnici WHERE radnik_id=$fRadnik LIMIT 1");
				$radnici = array(
					"polje_id" => array($fRadnik),
					"polje_ime" => array($fRadnikIme),
					"polje_prezime" => array($fRadnikPrezime)
				);
			}else{
				if (preg_match('/^\d+$/',$fGrupa) && $fGrupa>=0){
					$res = $this->db->select2("SELECT radnik_id, radnik_ime,radnik_prezime FROM radnici WHERE radnik_gr_id=$fGrupa AND radnik_akt=1");
					if ($res!=false) {
						$radgr = array();
						foreach ($res as $k=>$row){
							$radgr["polje_id"][$k] = $row["radnik_id"];
							$radgr["polje_ime"][$k] = $row["radnik_ime"];
							$radgr["polje_prezime"][$k] = $row["radnik_prezime"];
						}
						$radnici = $radgr;
					}else{
						$radnici = array("polje_id"=>array());
					}
					//list($fRadnikID,$fRadnikIme,$fRadnikPrezime) = $this->db->getRow("SELECT radnik_ime,radnik_prezime FROM radnici WHERE radnik_id=$fRadnik LIMIT 1");
				}else{
					$radnici = $aktivni_radnici;
				}
			}
			foreach ($radnici["polje_id"] as $k=>$r){
				
				if (!in_array($radnici["polje_ime"][$k],$this->escapedWorkers)) {
				
					$data = $this->get_EvidencijaTimes($r,$fDate_start,$fDate_end);
					if ($data!== false) {
						foreach ($data as $m=>$d){
							if ($fOpravdanje<0 || ($fOpravdanje>0 && $d["opravdanje"]==$fOpravdanje)) {
								$ids[] = $d["id"];
								
								/*$prev = $radids[count($radids)-1];
								$pp = intval($prev);
								//print_rarray($radids);
								if ($pp<1 && $m>0) {
									$counter = count($radids)-1;
									echo $counter." -> ";
									while ($counter>0) {
										if (intval($radids[$counter])>0) {
											$prev = $radids[$counter];
											break;
										}
										$counter--;
									}
								}
								
								echo "$m. $prev<br />";*/
								
								
								if ($m>0) {
									$radids[] = "";
									$rad_ime[] = "";
									$rad_prezime[] = "";
								}else{
									$radids[] = $r;
									$rad_ime[] = $radnici["polje_ime"][$k];
									$rad_prezime[] = $radnici["polje_prezime"][$k];
								}
								$rad_day[] = $this->weekdays[date("N",$d["time_in"])-1]." - ".date("d.m.Y",$d["time_in"]);
								
								if ($d["nije_dosao"]==1) {
									$in[] = "nije došao";
									$opravdanje = $this->get_EvidencijaOpravdanjeName($d["opravdanje"]);
									$out[] = $opravdanje;
								}else{
									$in[] = date("H:i",$d["time_in"]);
									$out[] = date("H:i",$d["time_out"]);
								}
								
								if ($d["opravdano"]>0) {
									$opravdano[] = '<img src="gui/check.gif" lat="check" />';
								}else{
									$opravdano[] = "";
								}
								
								$napomena[] = nl2br($d["napomena"]);
								if (strlen($d["napomena"])>0) {
									$napomena_class[] = "napomena";
								}else{
									$napomena_class[] = "";
								}
							}
						}
					}else{
						if ($fOpravdanje<1) {
							$ids[] = "";
							$radids[] = $r;
							$rad_ime[] = $radnici["polje_ime"][$k];
							$rad_prezime[] = $radnici["polje_prezime"][$k];
							$rad_day[] = "";
							$in[] = "";
							$out[] = "";
							$opravdano[] = "";
							$napomena[] = "";
							$napomena_class[] = "";
						}
					}
				}
			}
			//echo count($ids)." -> ".count($radids);
			$this->assign("eID",$ids);
			$this->assign("eRadid",$radids);
			$this->assign("eRadIme",$rad_ime);
			$this->assign("eRadPrezime",$rad_prezime);
			$this->assign("eRadDay",$rad_day);
			$this->assign("eRadIn",$in);
			$this->assign("eRadOut",$out);
			$this->assign("eRadOpravdano",$opravdano);
			$this->assign("eRadNapomena",$napomena);
			$this->assign("eRadNapomenaClass",$napomena_class);
		}
		
		echo $this->parseTemplate("evidencija_dolazaka/browse.tpl");
	}
	function save(){
		$len = getFromInput("length");
		$post = $_POST;
		
		$user = $this->getUserId();
		$time = time();
		$ip   = $_SERVER['REMOTE_ADDR'];
		
		$date = getFromInput("date");
		$tmp = explode(".",$date);
		$day  = $tmp[0];
		$month  = $tmp[1];
		$year  = $tmp[2];
		$time = date2int($date);
		
		foreach ($post as $name=>$val){
			if (preg_match('/_/',$name)) {
				
				$val = utf8RawUrlDecode($val);
				$val = utf8RawChrDecode($val);
				
				$last_ = strrpos($name,"_");
				$indx = substr($name,$last_+1);
				$key = substr($name,0,$last_);
				$this->rows[$indx][$key] = $val;
			}
		}
		$queries = array();
		$ok = true;
		foreach ($this->rows as $row){
			$radnik	= $row["radnik"];
			$excuse	= $row["napomena"];
			$nije_dosao = $row["nonpresent"]=="on"?1:0;
			$opravdano = $row["opravdano"]=="on"?1:0;
			$opravdanje = $row["opravdanje"];
			if ($nije_dosao) {
				$timein = makeUnixTime(0,0,0,$month,$day,$year);
				$timeout= $timein;
			}else{
				$timein	= makeUnixTime($row["time_in_H"],$row["time_in_M"],0,$month,$day,$year);
				$timeout= makeUnixTime($row["time_out_H"],$row["time_out_M"],0,$month,$day,$year);
			}
			
			$check_vreme = ($nije_dosao>0 || ($nije_dosao==0&&(intval($row["time_in_H"])>0 && intval($row["time_out_H"]))>0));
			$check = $radnik>0 && $check_vreme && preg_match('/\d{1,2}\.\d{1,2}\.\d{1,3}/',$date);
			
			//$dbg .= var_export($check_vreme,true)."\t".$check."\n";
			
			if($check){
				$queries[count($queries)] = ""
					."INSERT INTO evidencija(user,time,ip,radnik,time_in,time_out,nije_dosao,opravdanje,opravdano,napomena) "
					."VALUES ($user,$time,'$ip',$radnik,'$timein','$timeout','$nije_dosao','$opravdanje','$opravdano','".addslashes($excuse)."')";
			}else{
				$ok = false;
				break;
			}
		}
		//echo "0{!}$dbg"; die();
		
		if ($ok) {
			$ok=true;
			
			foreach ($queries as $q){
				if (!$this->db->alterTable($q)) {
					$ok=false;
					break;
				}
			}
			if ($ok) {
				echo "1{!}Unos izvršen{!}";
			}else{
				echo "0{!}".mysql_error()."{!}";
			}
		}else{
			echo "0{!}Niste popunili obavezna polja!{!}";
		}
	}
	
	function monthlyReport(){
		
		$date = trim(getFromInput("date"));
		$this->time_offset = getFromInput("time_offset");
		
		$time_offsets = array("-2","-1","0","1","2");
		if (strlen($this->time_offset)>0) {
			foreach ($time_offsets as $k=>$to){
				if ($to==$this->time_offset) {
					$time_offsets_selected[$k] = " selected";
				}else{
					$time_offsets_selected[$k] = "";
				}
			}
		}else{
			$time_offsets_selected = array("",""," selected","","");
		}
		$this->assign("time_offset",$time_offsets);
		$this->assign("time_offset_selected",$time_offsets_selected);
		
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$date)) {
			
			$tmp = explode(".",$date);
			$day = $tmp[0];
			$month = $tmp[1];
			$year = $tmp[2];
			$this->assign("f_date","$day.$month.$year");
			
			$date_start = makeUnixTime(0,0,0,$month,1,$year);
			$date_end = makeUnixTime(0,0,0,($month+1),1,$year)-1;
			
			$radnici = $this->get_Radnici(null,1);
			
			$this->assign("after8",$this->_reportLate($date_start,$date_end,$radnici));
			$this->assign("after1630",$this->_reportOvertime($date_start,$date_end,$radnici));
			$this->assign("before16",$this->_reportEarly($date_start,$date_end,$radnici));
			$this->assign("notpresent",$this->_reportNotPresent($date_start,$date_end,$radnici));
		}
		echo $this->parseTemplate("evidencija_dolazaka/reports/monthly_report.tpl");
	}
	
	function _reportLate($date_start,$date_end,$radnici=null){
		$i=0;
		foreach ($radnici["polje_id"] as $k=>$v){
			$radid	= $v;
			$ime	= $radnici["polje_ime"][$k];
			$prezime= $radnici["polje_prezime"][$k];
			
			if (!in_array($ime,$this->escapedWorkers)) {
				$q = "SELECT * "
					."FROM evidencija "
					."WHERE time_in>=$date_start AND time_in<=$date_end "
					."AND radnik=$radid";
				$res = $this->db->select2($q);
				if ($res !== false) {
					$stat_days=0;
					$stat_min=0;
					foreach ($res as $row){
						$row["time_out"] += $this->time_offset*(60*60);
						$row["time_in"] += $this->time_offset*(60*60);
						
						$hour = intval(date("H",$row["time_in"]));
						$minute = intval(date("i",$row["time_in"]));
						$wd = intval(date("N",$row["time_in"]));
						
						if ($wd<6) {
							$start_hour=8;
						}else{
							$start_hour=9;
						}
						
						if ($hour>$start_hour || ($hour>($start_hour-1) && $minute>0) ) {
							$i++;
							$ids[]		= $i;
							$stats_wd[] = $wd;
							
							$opravdano[] = $row["opravdano"];
							if ($row["opravdano"]>0) {
								$row["napomena"] = "opravdano, ".$row["napomena"];
							}
							
							if(count($radids)==0 || (count($radids)>0 && $this->getLast($radids)!=$radid)){
								$radids[]	= $radid;
								$names[]	= $ime;
								$last[]		= $prezime;
							}else{
								$radids[]	= "";
								$names[]	= "";
								$last[]		= "";
							}
							$stats[]	= "";
							
							$dates[]	= $this->weekdays[$wd-1].", ".date("d",$row["time_in"]);
							$in[]		= date("H:i",$row["time_in"]);
							$out[]		= date("H:i",$row["time_out"]);
							
							$row["napomena"] = str_replace("\n"," ",$row["napomena"]);
							if($row["nije_dosao"]){
								$q = "SELECT name FROM evidencija_opravdanja WHERE id=".$row["opravdanje"]." LIMIT 1";
								$opr = $this->db->getOne($q);
								if (strlen($row["napomena"])) {
									$opr .= "<br />".$row["napomena"];
								}
								$opravdanje[] = $opr;
							}else{
								$opravdanje[] = $row["napomena"];
							}
						}
					}
				}
			}
		}
		if($i>0){
			foreach ($in as $k=>$v){
				if(strlen($radids[$k])){
					$rad=$radids[$k];
				}
				$tmp=explode(":",$v);
				$opr = intval($opravdano[$k]);
				if ($opr<1) {
					$s[$rad][] = array(
						"h" => intval($tmp[0]),
						"m" => intval($tmp[1]),
						"wd"=> $stats_wd[$k]
					);
				}else{
					$s[$rad][] = array(
						"h" => ($stats_wd[$k]<6)?8:9,
						"m" => 0,
						"wd"=> $stats_wd[$k]
					);
				}
			}
			foreach ($stats as $k=>$v){
				$r=$radids[$k];
				if ($r>0) {
					$s_d=0;
					$s_m=0;
					if (count($s[$r])) {
						foreach ($s[$r] as $m){
							$sh=8;
							if ($m["wd"]>5) {$sh=9;}
							$s_d++;
							$s_m += ((($m["h"]*60)+$m["m"])-($sh*60));
							//echo "$k: ".((($m["h"]*60)+$m["m"])-($sh*60))."<br />";
						}
						if ($s_m>=60) {
							$s_mh = bcdiv($s_m,60,0); $s_mh = $s_mh<10?"0".$s_mh:$s_mh;
							$s_mm = $s_m%60; $s_mm=$s_mm>9?$s_mm:"0".$s_mm;
							$s_m = "$s_mh:$s_mm";
						}else{
							
							$s_m = "00:".($s_m>9?$s_m:"0".$s_m);
						}
						$stats[$k] = "$s_m [$s_d]";
					}
				}
			}
			
			$this->assign("row_id",$ids);
			$this->assign("radnik_ime",$names);
			$this->assign("radnik_prezime",$last);
			$this->assign("date",$dates);
			$this->assign("time_in",$in);
			$this->assign("time_out",$out);
			$this->assign("opravdano",$opravdano);
			$this->assign("opravdano_txt",$opravdano_txt);
			$this->assign("opravdanje",$opravdanje);
			$this->assign("stats",$stats);
			
			return $this->parseTemplate("evidencija_dolazaka/reports/late.tpl");
		}else{
			return "<h3>Kašnjenja</h3><p>Nema rezultata.</p>";
		}
	}
	function _reportOvertime($date_start,$date_end,$radnici=null){
		$i=0;
		foreach ($radnici["polje_id"] as $k=>$v){
			$radid	= $v;
			$ime	= $radnici["polje_ime"][$k];
			$prezime= $radnici["polje_prezime"][$k];
			
			if (!in_array($ime,$this->escapedWorkers)) {
				$q = "SELECT * "
					."FROM evidencija "
					."WHERE time_out>=$date_start AND time_out<=$date_end "
					."AND radnik=$radid";
				$res = $this->db->select2($q);
				if ($res !== false) {
					$stat_days=0;
					$stat_min=0;
					foreach ($res as $row){
						$row["time_out"] += $this->time_offset*(60*60);
						$row["time_in"] += $this->time_offset*(60*60);
						
						$hour = intval(date("H",$row["time_out"]));
						$minute = intval(date("i",$row["time_out"]));
						$wd = date("N",$row["time_in"]);
						
						if ($wd<6) {
							$end_hour=16;
						}else if($wd==6){
							$end_hour=15;
						}else{
							$end_hour=0;
						}
						
						if ($hour>$end_hour || ($hour>($end_hour-1) && $minute>15) ) {
							$i++;
							$ids[]		= $i;
							$stats_wd[] = $wd;
							if(count($radids)==0 || (count($radids)>0 && $this->getLast($radids)!=$radid)){
								$radids[]	= $radid;
								$names[]	= $ime;
								$last[]		= $prezime;
							}else{
								$radids[]	= "";
								$names[]	= "";
								$last[]		= "";
							};
							$stats[]	= "";
							
							$dates[]	= $this->weekdays[$wd-1].", ".date("d",$row["time_in"]);
							$in[]		= date("H:i",$row["time_in"]);
							$out[]		= date("H:i",$row["time_out"]);
							
							$row["napomena"] = str_replace("\n"," ",$row["napomena"]);
							if($row["nije_dosao"]){
								$q = "SELECT name FROM evidencija_opravdanja WHERE id=".$row["opravdanje"]." LIMIT 1";
								$opr = $this->db->getOne($q);
								if (strlen($row["napomena"])) {
									$opr .= "<br />".$row["napomena"];
								}
								$opravdanje[] = $opr;
							}else{
								$opravdanje[] = $row["napomena"];
							}
						}
					}
				}
			}
			
		}
		
		if($i>0){
			foreach ($out as $k=>$v){
				if(strlen($radids[$k])){
					$rad=$radids[$k];
				}
				$tmp=explode(":",$v);
				
				$endHour = $stats_wd[$k]<6?16:15;
				if(intval($tmp[0])>$end_hour || (intval($tmp[0])==$endHour && intval($tmp[1]>15))){
					$s[$rad][] = array(
						"h" => intval($tmp[0]),
						"m" => intval($tmp[1]),
						"wd"=> $stats_wd[$k]
					);
				}
			}
			foreach ($stats as $k=>$v){
				$r=$radids[$k];
				if ($r>0) {
					$s_d=0;
					$s_m=0;
					if (count($s[$r])) {
						foreach ($s[$r] as $m){
							$eh=16;
							if ($m["wd"]>5) {$eh=15;}
							$s_d++;
							$s_m += ((($m["h"]*60)+$m["m"])-($eh*60))-15;
						}
						if ($s_m>=60) {
							$s_mh = bcdiv($s_m,60,0); $s_mh = $s_mh<10?"0".$s_mh:$s_mh;
							$s_mm = $s_m%60; $s_mm=$s_mm>9?$s_mm:"0".$s_mm;
							$s_m = "$s_mh:$s_mm";
						}else{
							$s_m = "00:".($s_m>9?$s_m:"0".$s_m);
						}
						$stats[$k] = "$s_m [$s_d]";
					}
				}
			}
			
			$this->assign("row_id",$ids);
			$this->assign("radnik_ime",$names);
			$this->assign("radnik_prezime",$last);
			$this->assign("date",$dates);
			$this->assign("time_in",$in);
			$this->assign("time_out",$out);
			$this->assign("opravdanje",$opravdanje);
			$this->assign("stats",$stats);
			
			return $this->parseTemplate("evidencija_dolazaka/reports/overtime.tpl");
		}else{
			return "<h3>Prekovremeni rad</h3><p>Nema rezultata.</p>";
		}
	}
	function _reportEarly($date_start,$date_end,$radnici=null){
		$i=0;
		foreach ($radnici["polje_id"] as $k=>$v){
			$radid	= $v;
			$ime	= $radnici["polje_ime"][$k];
			$prezime= $radnici["polje_prezime"][$k];
			
			if (!in_array($ime,$this->escapedWorkers)) {
				$q = "SELECT * "
					."FROM evidencija "
					."WHERE time_out>=$date_start AND time_out<=$date_end "
					."AND opravdanje<1 "
					."AND radnik=$radid";
				$res = $this->db->select2($q);
				if ($res !== false) {
					foreach ($res as $row){
						$row["time_out"] += $this->time_offset*(60*60);
						$row["time_in"] += $this->time_offset*(60*60);
						
						$hour = intval(date("H",$row["time_out"]));
						$minute = intval(date("i",$row["time_out"]));
						$wd = date("N",$row["time_in"]);
						
						if ($wd<6) {
							$end_hour=16;
						}else{
							$end_hour=15;
						}
						
						if ($hour<$end_hour) {
							$i++;
							$ids[]		= $i;
							if(count($radids)==0 || (count($radids)>0 && $radids[count($radids)-1]!=$radid)){
								$radids[]	= $radid;
								$names[]	= $ime;
								$last[]		= $prezime;
							}else{
								$radids[]	= "";
								$names[]	= "";
								$last[]		= "";
							}
							
							$stats[]	= "";
							$stats_wd[] = $wd;
							
							$opravdano[] = $row["opravdano"];
							if ($row["opravdano"]>0) {
								$row["napomena"] = "opravdano, ".$row["napomena"];
							}
							
							$dates[]	= $this->weekdays[$wd-1].", ".date("d",$row["time_in"]);
							$in[]		= date("H:i",$row["time_in"]);
							$out[]		= date("H:i",$row["time_out"]);
							
							$row["napomena"] = str_replace("\n"," ",$row["napomena"]);
							if($row["nije_dosao"]){
								$q = "SELECT name FROM evidencija_opravdanja WHERE id=".$row["opravdanje"]." LIMIT 1";
								$opr = $this->db->getOne($q);
								if (strlen($row["napomena"])) {
									$opr .= "<br />".$row["napomena"];
								}
								$opravdanje[] = $opr;
							}else{
								$opravdanje[] = $row["napomena"];
							}
						}
					}
				}
			}
			
		}
		if($i>0){
			foreach ($in as $k=>$v){
				if(strlen($radids[$k])){
					$rad=$radids[$k];
				}
				$tmp=explode(":",$v);
				$opr = intval($opravdano[$k]);
				if ($opr<1) {
					$s[$rad][] = array(
						"h" => intval($tmp[0]),
						"m" => intval($tmp[1]),
						"wd"=> $stats_wd[$k]
					);
				}else{
					$s[$rad][] = array(
						"h" => ($stats_wd[$k]<6)?8:9,
						"m" => 0,
						"wd"=> $stats_wd[$k]
					);
				}
			}
			foreach ($stats as $k=>$v){
				$r=$radids[$k];
				if ($r>0) {
					$s_d=0;
					$s_m=0;
					if (count($s[$r])) {
						foreach ($s[$r] as $m){
							$sh=8;
							if ($m["wd"]>5) {$sh=9;}
							$s_d++;
							$s_m += ((($m["h"]*60)+$m["m"])-($sh*60));
							//echo "$k: ".((($m["h"]*60)+$m["m"])-($sh*60))."<br />";
						}
						if ($s_m>=60) {
							$s_mh = bcdiv($s_m,60,0); $s_mh = $s_mh<10?"0".$s_mh:$s_mh;
							$s_mm = $s_m%60; $s_mm=$s_mm>9?$s_mm:"0".$s_mm;
							$s_m = "$s_mh:$s_mm";
						}else{
							
							$s_m = "00:".($s_m>9?$s_m:"0".$s_m);
						}
						$stats[$k] = "$s_m [$s_d]";
					}
				}
			}
			$this->assign("row_id",$ids);
			$this->assign("radnik_ime",$names);
			$this->assign("radnik_prezime",$last);
			$this->assign("date",$dates);
			$this->assign("time_in",$in);
			$this->assign("time_out",$out);
			$this->assign("opravdanje",$opravdanje);
			$this->assign("stats",$stats);
			
			return $this->parseTemplate("evidencija_dolazaka/reports/early.tpl");
		}else{
			return "<h3>Odlazak pre kraja radnog vremena</h3><p>Nema rezultata.</p>";
		}
	}
	function _reportNotPresent($date_start,$date_end,$radnici=null){
		$i=0;
		foreach ($radnici["polje_id"] as $k=>$v){
			$radid	= $v;
			$ime	= $radnici["polje_ime"][$k];
			$prezime= $radnici["polje_prezime"][$k];
			
			if (!in_array($ime,$this->escapedWorkers)) {
				$q = "SELECT * "
					."FROM evidencija "
					."WHERE time_out>=$date_start AND time_out<=$date_end "
					."AND nije_dosao=1 "
					."AND radnik=$radid";
				$res = $this->db->select2($q);
				if ($res !== false) {
					foreach ($res as $row){
						
						$row["time_out"] += $this->time_offset*(60*60);
						$row["time_in"] += $this->time_offset*(60*60);
						
						$hour = intval(date("H",$row["time_out"]));
						$minute = intval(date("i",$row["time_out"]));
						$wd = date("N",$row["time_in"]);
						
						$i++;
						$ids[]		= $i;
						if(count($radids)==0 || (count($radids)>0 && $this->getLast($radids)!=$radid)){
							$radids[]	= $radid;
							$names[]	= $ime;
							$last[]		= $prezime;
						}else{
							$radids[]	= "";
							$names[]	= "";
							$last[]		= "";
						}
						$dates[]	= $this->weekdays[$wd-1].", ".date("d",$row["time_in"]);
						$in[]		= date("H:i",$row["time_in"]);
						$out[]		= date("H:i",$row["time_out"]);
						
						$row["napomena"] = str_replace("\n"," ",$row["napomena"]);
						if($row["nije_dosao"]){
							$q = "SELECT name FROM evidencija_opravdanja WHERE id=".$row["opravdanje"]." LIMIT 1";
							$opr = $this->db->getOne($q);
							if (strlen($row["napomena"])) {
								$opr .= "<br />".$row["napomena"];
							}
							$opravdanje[] = $opr;
						}else{
							$opravdanje[] = $row["napomena"];
						}
					}
				}
			}
			
		}
		if($i>0){
			$this->assign("row_id",$ids);
			$this->assign("radnik_ime",$names);
			$this->assign("radnik_prezime",$last);
			$this->assign("date",$dates);
			$this->assign("time_in",$in);
			$this->assign("time_out",$out);
			$this->assign("opravdanje",$opravdanje);
			
			return $this->parseTemplate("evidencija_dolazaka/reports/notpresent.tpl");
		}else{
			return "<h3>Odsustvo</h3><p>Nema rezultata.</p>";
		}
	}
	
	function suboteReport(){
		$month = getFromInput("month");
		$year = getFromInput("year");
		
		$polja = $this->get_Months($month);
			$this->assign("month_id",$polja["polje_id"]);
			$this->assign("month",$polja["polje_ime"]);
			$this->assign("selected_month",$polja["polje_selected"]);
			
		$polja = $this->get_Years($year);
			$this->assign("year",$polja["polje_id"]);
			$this->assign("selected_year",$polja["polje_selected"]);
			
		if ($month>0 && $year>0) {
			$radnici = $this->get_Radnici(null,1);
			foreach ($radnici["polje_id"] as $k=>$radnik_id){
				$start = makeUnixTime(0,0,0,$month,1,$year);
				$end = makeUnixTime(0,0,0,$month>11?1:($month+1),1,$month>11?($year+1):$year);
				
				//echo $radnik["radnik_ime"]." ".$radnik["radnik_prezime"]." - $start-$end<br />";
				
				$q = "SELECT count(*) FROM evidencija WHERE radnik=$radnik_id AND time_in>=$start AND time_out<=$end AND FROM_UNIXTIME(time_in,'%w')=6";
				$num = $this->db->getOne($q);
				$radnik_ime[] = $radnici["polje_ime"][$k];
				$radnik_prezime[] = $radnici["polje_prezime"][$k];
				$nums[] = $num>0?$num:"";
			}
			$this->assign("radnik_ime",$radnik_ime);
			$this->assign("radnik_prezime",$radnik_prezime);
			$this->assign("num",$nums);
		}
		
		echo $this->parseTemplate("evidencija_dolazaka/subote.tpl");
	}
	
	function deleteEvidencija(){
		$id = getFromInput("id");
		
		if ($this->db->alterTable("DELETE FROM evidencija WHERE id=$id LIMIT 1")) {
			echo "Brisanje izvršeno";
		}else{
			echo mysql_error();
		}
	}
	
	
	
	/******************     GET FUNCS     ***********************/
	function get_EvidencijaTimes($radid,$time_start,$time_end){
		$q = "SELECT e.id, radnik_id, radnik_ime, radnik_prezime, time_in, time_out, nije_dosao, opravdanje, opravdano, napomena "
			."FROM evidencija e, radnici r "
			."WHERE e.radnik=r.radnik_id "
			."AND radnik_id=$radid "
			."AND time>=$time_start AND time<$time_end "
			."GROUP BY time_in "
			."ORDER BY e.time DESC";
		$res = $this->db->select2($q);
		if ($res != false) {
			$ret = array();
			foreach ($res as $k=>$row){
				foreach ($row as $m=>$n){
					$ret[$k][$m] = $n;
				}
			}
			return $ret;
		}else{
			return false;
		}
	}
	function get_EvidencijaOpravdanja($id=null){
		$ret = array();
		$res = $this->db->select2("SELECT * FROM evidencija_opravdanja ORDER BY name");
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_ime"][$k] = $v["name"];
				if ($v["id"]==$id) {
					$ret["polje_selected"][$k] = " selected";
				}else{
					$ret["polje_selected"][$k] = "";
				}
			}
		}
		return $ret;
	}
	function get_EvidencijaOpravdanjeName($id){
		return $this->db->getOne("SELECT name FROM evidencija_opravdanja WHERE id=$id LIMIT 1");
	}
	function get_Months($month=null){
		$months = array(
			1	=> "januar",
			2	=> "februar",
			3	=> "mart",
			4	=> "april",
			5	=> "maj",
			6	=> "jun",
			7	=> "jul",
			8	=> "avgust",
			9	=> "septembar",
			10	=> "oktobar",
			11	=> "novembar",
			12	=> "decembar"
		);
		$ret = array();
		foreach ($months as $k=>$v){
			$key = count($ret["polje_id"]);
			$ret["polje_id"][$key] = $k;
			$ret["polje_ime"][$key] = $v;
			if ($month == $k) {
				$ret["polje_selected"][$key] = " selected";
			}else{
				$ret["polje_selected"][$key] = "";
			}
		}
		return $ret;
	}
	function get_Years($year=null){
		$theyear = date("Y");
		for ($i=$theyear; $i>=($theyear-10); $i--){
			$years[] = $i;
		}
		foreach ($years as $k=>$v){
			$ret["polje_id"][$k] = $v;
			if ($year == $v) {
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	
	
	/******************     PRIVATE FUNCS     ***********************/
	function message($str){
		$this->assign("message_display","block");
		$this->assign("message",$str);
	}	
	function spitGUIPage(){
		$this->db = new db_functions();
		$q = "SELECT users.username, radnici.radnik_ime, radnici.radnik_prezime, users.last_login, groups.gname "
			."FROM radnici, users,groups "
			."WHERE users.gid=groups.gid "
			."AND radnici.radnik_id=users.radnik_id "
			."AND users.id='".$_COOKIE['ypu']."'";
		$res = $this->db->getRow($q);
		
		$this->assign("user",$res[0]);
		$this->assign("user_ime",$res[1]);
		$this->assign("user_prezime",$res[2]);
		$this->assign("user_last_login",date($this->getFromConfig("long_date"),$res[3]));
		$this->assign("user_level",$res[4]);
		$this->assign("ip_address",$_SERVER['REMOTE_ADDR']);
		
		$this->assign("content",$this->parseTemplate($this->_content));
		$this->assign("main_menu",$this->parseTemplate("evidencija_dolazaka/menu.tpl"));
		$this->_container = "evidencija_dolazaka/main.tpl";
		$this->displayPage();
	}
	function getLast($arr){
		$len = count($arr);
		$ret = "";
		for ($i=$len-1; $i>=0; $i--){
			if (strlen($arr[$i])) {
				$ret = $arr[$i];
				break;
			}
		}
		return $ret;
	}
}

?>